//
//  SearchResultPackageCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/22.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "SearchResultPackageCell.h"

@implementation SearchResultPackageCell

+ (CGFloat)calculateHeightWithWidth:(CGFloat)collectionWeidth {

    CGFloat height = ((collectionWeidth-40)/2) + 100;
    return height;
}

- (void)fillCellWithModel:(CommonPackageModel *)model {
    
    NSString *mainUrl = [TT_OSSManager reSourceUrl:model.fangUrl type:BA_resizeType_pro];
    
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:mainUrl] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
       
        if (image) {
         
            if (cacheType != SDImageCacheTypeMemory) {
                [UIView transitionWithView:self.mainImageView duration:0.61 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                    [self.mainImageView setImage:image];
                    self.mainImageView.alpha = 1.0;
                } completion:nil];
            } else {
                self.mainImageView.alpha = 1.0;
                [self.mainImageView setImage:image];
            }
        }
    }];
    
    
    
    NSString *iconUrl = [TT_OSSManager reSourceUrl:model.logoUrl type:BA_resizeType_plus];
    [self.storeIcon sd_setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    self.storeName.text = model.sellerName;
    
    self.mainTitle.text = model.goodsName;
    
    
    double packageCommission = [BA_BusinessHelper shared].userProportion*0.01;
    double tax = [BA_BusinessHelper shared].taxPoint *0.01;

    NSString *doubleStr = [NSString stringWithFormat:@"%.0f",model.goodsActivePrice*packageCommission*(1-tax)];
    NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithString:doubleStr];
    self.fanxianLab.text = [NSString stringWithFormat:@"  ¥%@ ",dn.stringValue];
    self.fanxianLab.hidden = NO;
    self.fanxianIcon.hidden = NO;
    
    if (packageCommission == 0) {
        self.fanxianLab.hidden = YES;
        self.fanxianIcon.hidden = YES;
    }

    self.priceLab.text = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:model.goodsActivePrice]];
    
    self.countLab.text = [NSString stringWithFormat:@"%ld",(long)model.goodsClickCount];
    
    if (model.sgType == 2) {
        self.packageTagLab.hidden = NO;
        self.packageTagBG.hidden = NO;
    } else {
        self.packageTagLab.hidden = YES;
        self.packageTagBG.hidden = YES;
    }
    
    self.fanxianLab.hidden = YES;
    self.fanxianIcon.hidden = YES;
}


- (void)awakeFromNib {
    [super awakeFromNib];

    self.labTag1.layer.cornerRadius = 2;
    self.labTag1.layer.borderWidth = 1.0f;
    self.labTag1.layer.borderColor = UIColorHex(0xd2d2d2).CGColor;
    
    self.labTag2.layer.cornerRadius = 2;
    self.labTag2.layer.borderWidth = 1.0f;
    self.labTag2.layer.borderColor = UIColorHex(0xd2d2d2).CGColor;

    self.fanxianLab.layer.backgroundColor = UIColorHex(0xffeee0).CGColor;
    self.fanxianLab.layer.cornerRadius = 3;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self pp_clipCorners:UIRectCornerAllCorners radius:10];
        [self.packageTagBG pp_clipCorners:UIRectCornerBottomRight radius:10];
        [self.storeIcon pp_clipCorners:UIRectCornerAllCorners radius:self.storeIcon.frame.size.height/2];
    });
}

@end
